# -*- coding: utf-8 -*-
import os
import time

from django.conf import settings

from django.http import HttpResponseRedirect

from django.utils.translation import ugettext as _
from django.utils.translation import activate
from django.utils.translation import get_language_info
from annoying.decorators import render_to
from django.http import HttpResponse

from mediabox.lira.utils import  set_messages

# TODO: Test me
def set_language(request, code):
    """ Switch language if such exists, otherwise redirect back with appropriate message """

    try:
        get_language_info(code)
        activate(code)
    except KeyError:
        msg_filename = os.path.join(settings.MSG_DIR, '%s.msg' % (time.time()))
        messages = [{'type': 'success', 'message': _('The system does not support this language for now.'), }]

        if set_messages(filename=msg_filename, messages=messages):
            request.session['msg_file'] = msg_filename

    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

@render_to('index.html')
def site_index(request):
    return {}
